<?php
class Core_Frontend_Module_Poi_Latest_Render extends Core_Module_Render{

    protected $name = 'poi_latest';

    public function render(array $parameters = array()) {
        $obj = new Kd_Object($parameters);
        $pois = App::getModel('foundation/collection_poi')
                    ->resetSelect()
                    ->fields('a.shop_name', 'a.id', 'a.photo', 'a.road_id')
                    ->limit($obj->getItemCount());
        if($obj->getSortCondition() == 'clickcount'){
            $obj->joinVisit()->desc('clickcount');
        }
        $pois->load();
        $nameStartIndex = 0;
        $maxRecords = count($pois);
        if($obj->getImageCount()){
            $obj->setIsShowImage(TRUE);
            $obj->setStartImageIndex(0);
            $obj->setEndImageIndex(min($maxRecords, $obj->getImageCount()));
            $nameStartIndex = max($obj->getEndImageIndex(), $nameStartIndex);
        }
        if($obj->getIntroductionCount()){
            $obj->setIsShowIntroduction(TRUE);
            $obj->setStartIntroductionIndex(0);
            $obj->setEndIntroductionIndex(min($maxRecords, $obj->getIntroductionCount()));
            $nameStartIndex = max($obj->getEndIntroductionIndex(), $nameStartIndex);
        }
        $obj->setStartNameIndex($nameStartIndex);
        $obj->setEndNameIndex(min($maxRecords, $obj->getItemCount()));
        $tmpl = $this->getTemplate()->assignMulti(array('pois' => $pois, 'parameters'=>$obj));
        return $tmpl->render($this->getTemplatePath($obj->getTemplateName(), $obj->getTemplateCode()));
    }
}

